<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING); 
//ini_set('display_errors', '1');

class Extension extends a_base {

	public $typeArr = array();

	public function __construct(){
		parent::__construct();
		$this->typeArr = array(
			'-1'=>"请选择",
			'1'=>'普通显示广告',
			'2'=>'Flash幻灯片广告',
			'3'=>'浮动广告',
			'4'=>'对联广告',
		);
	}

	public function index()
	{
		$where = "adpositionid` !=\"\" ";
		$search = $this->input->post();
		if(!empty($search)){
			foreach ($search as $key => $value) {
				if($value == ''){
					unset($search[$key]);
					continue;
				}else{
					if($key == 'typeid' && $value == '-1'){
						unset($search[$key]);
						continue;
					}
					if($key == 'adpositionname' ){
						//$this->db->like('title', 'match', 'both');
						$where .= " AND `$key` LIKE '%$value%'";
					}elseif($key == 'isclose' || $key == 'typeid'){
						$where .= " AND `$key` = '$value'";
					}
				}
			}
		}
		$page = isset($_GET['page'])?$_GET['page']:1;//当前页
		$cntArr = $this->m_public->getRowInfo('adposition',array(),'count(1) as cnt');
		$arr = array (
			'page' => $page,//当前页
		    'page_size' => 10,     //分页总大小
		    'total' => $cntArr['cnt']//总记录数
		);
		$start_page = ($page-1)*10;
		$data['list'] = $this->m_public->getPageAllInfo('adposition',$where,10,$start_page);
		$data['admin'] = $this->m_public->getAllInfo('admin',array(),'adminid,adminname');
		// print_R($data['list']);
		$data['pagination'] = new pagination($arr);
		$this->load->view('extension/index',$data);
	}

	public function add_extension($adpositionid='')
	{
		if($adpositionid){
			$data['info'] = $this->m_public->getRowInfo('adposition',array('adpositionid'=>$adpositionid));
			$data['admin'] = $this->m_public->getAllInfo('admin',array(),'adminid,adminname');
		}else{
			$data['info'] = '';
		}
		//$data['typeArr'] = $this->typeArr;
		$this->load->view('extension/add_extension',$data);
	}

	public function delete_info($url,$parmykey,$table,$ids)
	{
		if(!$ids){
			redirect("extension/$url");
		}
		$idArr = explode('.', $ids);
		foreach($idArr as $k=>$v){
			$res = $this->m_public->delInfo($table,array($parmykey=>$v));
		}
		redirect("extension/$url");
	}

	public function edit_close($url,$parmykey,$table,$val,$ids)
	{
		if(!$ids){
			redirect(b_url("extension/$url"));
		}
		$idArr = explode('.', $ids);
		foreach($idArr as $k=>$v){
			$data['isclose'] = $val;
			$this->m_public->editInfo(array($parmykey=>$v),$data,$table);
		}
		redirect("extension/$url");
	}

	public function extension_action($adpositionid='')
	{
		$info = $this->input->post();
		if($adpositionid){
			$info['adminid'] = $this->session->userdata['user_id'];
			$this->m_public->editInfo(array('adpositionid'=>$adpositionid),$info,'adposition');
		}else{
			if(!empty($info)){
				$info['addtime'] = date('Y-m-d H:i:s');
				$info['adminid'] = $this->session->userdata['user_id'];
				$this->m_public->addRowInfo('adposition',$info);
			}
		}
		redirect('extension');
	}

	public function ad(){
		$where = "ad.adid` !=\"\" ";
		$this->load->model('m_extension');
		$search = $this->input->post();
		if(!empty($search)){
			foreach ($search as $key => $value) {
				if($value == ''){
					unset($search[$key]);
					continue;
				}else{
					if($key == 'adpositionid' && $value == '-1'){
						unset($search[$key]);
						continue;
					}
					if($key == 'adname' ){
						//$this->db->like('title', 'match', 'both');
						$where .= " AND `t_ad`.`$key` LIKE '%$value%'";
					}elseif($key == 'isclose' || $key == 'adpositionid'){
						$where .= " AND `t_ad`.`$key` = '$value'";
					}
				}
			}
		}
		$page = isset($_GET['page'])?$_GET['page']:1;//当前页
		$cntArr = $this->m_public->getRowInfo('ad',array(),'count(1) as cnt');
		$arr = array (
			'page' => $page,//当前页
		    'page_size' => 10, //分页总大小
		    'total' => $cntArr['cnt']//总记录数
		);
		$start_page = ($page-1)*10;
		$data['list'] = $this->m_extension->get_ad_info($where,10,$start_page);
		$data['admin'] = $this->m_public->getAllInfo('admin',array(),'adminid,adminname');
		$data['adposition'] = $this->m_public->getAllInfo('adposition',array(),'adpositionid,adpositionname');
		//print_R($data['list']);
		$data['pagination'] = new pagination($arr);
		$this->load->view('extension/ad',$data);
	}

	public function add_ad($adid='')
	{
		$data['adposition'] = $this->m_public->getAllInfo('adposition',array(),'adpositionid,adpositionname');
		if($adid){
			$data['info'] = $this->m_public->getRowInfo('ad',array('adid'=>$adid));
		}else{
			$data['info'] = '';
		}
		//$data['typeArr'] = $this->typeArr;
		$this->load->view('extension/add_ad',$data);
	}

	public function ad_action($adid='')
	{
		$info = $this->input->post();
		if($adid){
			$info['adminid'] = $this->session->userdata['user_id'];
			$this->m_public->editInfo(array('adid'=>$adid),$info,'ad');
		}else{
			if(!empty($info)){
				$info['addtime'] = date('Y-m-d H:i:s');
				$info['adminid'] = $this->session->userdata['user_id'];
				$this->m_public->addRowInfo('ad',$info);
			}
		}
		redirect('extension/ad');
	}

	public function link(){
		$where = "link.linkid` !=\"\" ";
		$this->load->model('m_extension');
		$search = $this->input->post();
		if(!empty($search)){
			foreach ($search as $key => $value) {
				if($value == ''){
					unset($search[$key]);
					continue;
				}else{
					if(($key == 'typeid' && $value == '-1') || ($key == 'configid' && $value == '-1')){
						unset($search[$key]);
						continue;
					}
					if($key == 'sitename' ){
						//$this->db->like('title', 'match', 'both');
						$where .= " AND `t_link`.`$key` LIKE '%$value%'";
					}elseif($key == 'isclose' || $key == 'typeid' || $key == 'configid'){
						$where .= " AND `t_link`.`$key` = '$value'";
					}
				}
			}
		}
		$page = isset($_GET['page'])?$_GET['page']:1;//当前页
		$cntArr = $this->m_public->getRowInfo('link',array(),'count(1) as cnt');
		$arr = array (
			'page' => $page,//当前页
		    'page_size' => 10, //分页总大小
		    'total' => $cntArr['cnt']//总记录数
		);
		$start_page = ($page-1)*10;
		$data['list'] = $this->m_extension->get_link_info($where,10,$start_page);
		$data['admin'] = $this->m_public->getAllInfo('admin',array(),'adminid,adminname');
		$data['config'] = $this->m_public->getAllInfo('config',array(),'configid,languagever');

		$data['pagination'] = new pagination($arr);
		$this->load->view('extension/link',$data);
	}

	public function add_link($linkid='')
	{
		$data['config'] = $this->m_public->getAllInfo('config',array(),'configid,languagever');
		if($linkid){
			$data['info'] = $this->m_public->getRowInfo('link',array('linkid'=>$linkid));
		}else{
			$data['info'] = '';
		}
		//$data['typeArr'] = $this->typeArr;
		$this->load->view('extension/add_link',$data);
	}

	public function link_action($linkid='')
	{
		$info = $this->input->post();
		if($linkid){
			$info['adminid'] = $this->session->userdata['user_id'];
			$this->m_public->editInfo(array('linkid'=>$linkid),$info,'link');
		}else{
			if(!empty($info)){
				$info['addtime'] = date('Y-m-d H:i:s');
				$info['adminid'] = $this->session->userdata['user_id'];
				$this->m_public->addRowInfo('link',$info);
			}
		}
		redirect('extension/link');
	}

    public function chat(){
        $post = $this->input->post();
        $where = array();
        if($post){
            foreach($post as $key=>$val){
                if($val != ''){
                    if($key == 'typeid' && $val != '-1'){
                        $where['chat.'.$key] = $val;
                    }elseif($key != 'typeid'){
                        $where['chat.'.$key] = $val;
                    }
                }
            }
        }
        $data['chat_type'] = $this->chat_type();
        $page = isset($_GET['page'])?$_GET['page']:1;//当前页
        $cntArr = $this->m_public->getRowInfo('chat as chat',$where,'count(1) as cnt');
        $arr = array (
            'page' => $page,//当前页
            'page_size' => 10, //分页总大小
            'total' => $cntArr['cnt']//总记录数
        );
        $data['pagination'] = new pagination($arr);
        $start_page = ($page-1)*10;
        $page_size = 20;
        $condition = array(
            'from' => 'chat as chat',
            'limit' => $page_size,
            'offset' => $start_page,
            'join' => array(
                'admin as admin' => 'chat.adminid=admin.adminid',
                'config as config' => 'chat.configid=config.configid'
            ),
            'where' => $where,
            'column' => 'chat.*,admin.adminname,config.languagever',
        );
        if($where){
            $data['filter'] = 1;
        }else{
            $data['filter'] = 0;
        }
        $data['list'] = $this->m_public->get_list($condition);

        $this->load->view('extension/chat',$data);
    }

    private function chat_type(){
        return array(
            '1' => 'QQ',
            '2' => 'MSN',
            '3' => 'Skype',
            '4' => '阿里旺旺'
        );
    }

    public function add_chat($chatid){
        $data['chat_type'] = $this->chat_type();
        $data['config'] = $this->m_public->getAllInfo('config');
        if($chatid){
            $data['info'] = $this->m_public->getRowInfo('chat',array('chatid'=>$chatid));
        }else{
            $data['info'] = array();
        }
        $this->load->view('extension/add_chat',$data);
    }

    public function chat_action($chatid){
        $post = $this->input->post();
        if($chatid){
            $this->m_public->editInfo(array('chatid'=>$chatid),$post,'chat');
        }else{
            $post['addtime'] = date('Y-m-d H:i:s',time());
            $post['adminid'] = $this->session->userdata['user_id'];
            $this->m_public->addRowInfo('chat',$post);
        }
        redirect('extension/chat');
    }

    public function data_backup(){
        $data = array();
        $this->load->view('extension/data_backup',$data);
    }

    public function back_up(){
        include_once('/backend/libraries/dbbak.php');
        include_once('/backend/libraries/tablebak.php');
        $database = $this->db->database;
        $user = $this->db->username;
        $pass = $this->db->password;
        $connectid = mysql_connect('localhost',$user,$pass);
        $backupDir = 'static/sql-backup';
        $DbBak = new dbbak($connectid,$backupDir);
        $DbBak->backupDb($database);
    }

}
